package com.sixmac.service.impl;

import com.sixmac.core.Constant;
import com.sixmac.dao.WaterPriceDao;
import com.sixmac.entity.WaterPrice;
import com.sixmac.service.WaterPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/5/30 0030 下午 5:24.
 */
@Service
public class WaterPriceServiceImpl implements WaterPriceService {

    @Autowired
    private WaterPriceDao waterPriceDao;

    @Override
    public List<WaterPrice> findAll() {
        return waterPriceDao.findAll();
    }

    @Override
    public Page<WaterPrice> find(int pageNum, int pageSize) {
        return waterPriceDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<WaterPrice> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public WaterPrice getById(int id) {
        return waterPriceDao.findOne(id);
    }

    @Override
    public WaterPrice deleteById(int id) {
        WaterPrice waterPrice = getById(id);
        waterPriceDao.delete(waterPrice);
        return waterPrice;
    }

    @Override
    public WaterPrice create(WaterPrice waterPrice) {
        return waterPriceDao.save(waterPrice);
    }

    @Override
    public WaterPrice update(WaterPrice waterPrice) {
        return waterPriceDao.save(waterPrice);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }
}